package com.china08.yxyapi.api.manage.sch.platform.attendance.statistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.attendance.YxyYxyTecAttendanceConfig;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendanceStatistics;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.attendance.YxyTecAttendanceConfigRepository;
import com.china08.yxyapi.repository.db.yxy.YxyHolidayRepository;
import com.china08.yxyapi.service.db.YxyTeaAttendanceStatisticsService;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class AttendAnceStatisticsExcelListController extends YxyApiBaseController {
    @Autowired
    YxyTeaAttendanceStatisticsService yxyTeaAttendanceStatisticsService;

    @Autowired
    YxyHolidayRepository yxyHolidayRepository;

    @Autowired
    YxyTecAttendanceConfigRepository yxyTecAttendanceConfigRepository;

    @ApiOperation("设置考勤配置")
    @RequestMapping(value = "/api_yw/manage/sch/platform/attendance/statistics/excelList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String times = "";
        String status = reqModel.status;
        Date month = reqModel.month;
        String keyWord = reqModel.keyWord;
        String schoolId = getProfile(request, response).getSchoolId();

        YxyYxyTecAttendanceConfig yxyTecAttendanceConfig = yxyTecAttendanceConfigRepository.findBySchoolId(schoolId);
        if (null != yxyTecAttendanceConfig) {
            times = yxyTecAttendanceConfig.getTimes();
        }
        List<YxyTeaAttendanceStatistics> list = yxyTeaAttendanceStatisticsService.findList(schoolId, times, status, month, keyWord);

        List<RespModel> returnList = new ArrayList<RespModel>();

        for (YxyTeaAttendanceStatistics yxyTeaAttendanceStatistics : list) {

            String dayOfDate = yxyTeaAttendanceStatistics.getDayOfDate() + "";
            String d1 = StringUtils.substring(dayOfDate, 0, 4);
            String d2 = StringUtils.substring(dayOfDate, 4, 6);
            String d3 = StringUtils.substring(dayOfDate, 6, 8);
            dayOfDate = d1 + "-" + d2 + "-" + d3;
            boolean isHoliday = yxyTeaAttendanceStatistics.getIsHoliday() == 1;
            String description = yxyTeaAttendanceStatistics.getHolidayDescription();

            String teacherId = yxyTeaAttendanceStatistics.getTeacherId();
            String teacherName = yxyTeaAttendanceStatistics.getTeacherName();
            Date signTime1 = yxyTeaAttendanceStatistics.getSignTime1();
            Date signTime2 = yxyTeaAttendanceStatistics.getSignTime2();
            Date signTime3 = yxyTeaAttendanceStatistics.getSignTime3();
            Date signTime4 = yxyTeaAttendanceStatistics.getSignTime4();
            String t1 = DateToolUtils.format(signTime1, "HH:mm:ss");
            String t2 = DateToolUtils.format(signTime2, "HH:mm:ss");
            String t3 = DateToolUtils.format(signTime3, "HH:mm:ss");
            String t4 = DateToolUtils.format(signTime4, "HH:mm:ss");

            Integer signStatus1 = yxyTeaAttendanceStatistics.getSignStatus1();
            Integer signStatus2 = yxyTeaAttendanceStatistics.getSignStatus2();
            Integer signStatus3 = yxyTeaAttendanceStatistics.getSignStatus3();
            Integer signStatus4 = yxyTeaAttendanceStatistics.getSignStatus4();

            RespModel respModel = new RespModel();
            respModel.times = times;
            respModel.dayOfDate = dayOfDate;
            respModel.teacherId = teacherId;
            respModel.teacherName = teacherName;
            respModel.signTime1 = t1;
            respModel.signTime2 = t2;
            respModel.signTime3 = t3;
            respModel.signTime4 = t4;
            respModel.signStatus1 = signStatus1;
            respModel.signStatus2 = signStatus2;
            respModel.signStatus3 = signStatus3;
            respModel.signStatus4 = signStatus4;
            respModel.isHoliday = isHoliday;
            respModel.description = description;
            returnList.add(respModel);
        }
        return returnList;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public Date month;
        public String status;
        public String keyWord;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String dayOfDate;
        public String teacherId;
        public String teacherName;
        public String signTime1;
        public String signTime2;
        public String signTime3;
        public String signTime4;
        public Integer signStatus1;
        public Integer signStatus2;
        public Integer signStatus3;
        public Integer signStatus4;
        public boolean isHoliday;
        public String description;
        public String times;
        public long total;
    }
}
